package com.android.hedgehog;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.scoreloop.client.android.core.controller.RequestController;
import com.scoreloop.client.android.core.controller.RequestControllerObserver;
import com.scoreloop.client.android.core.controller.ScoresController;
import com.scoreloop.client.android.core.controller.UserController;
import com.scoreloop.client.android.core.model.Score;
import com.scoreloop.client.android.core.model.ScoreFormatter;
import com.scoreloop.client.android.core.model.User;

public class LeaderBoard4 extends Activity {
	private final static int DIALOG_ERROR = 0;
	private final static int DIALOG_PROGRESS = 1;
	ScoresController scoresController;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.leaderboard2);
	}

	// handler to create our dialogs
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_PROGRESS:
			return ProgressDialog.show(LeaderBoard4.this, "", getString(R.string.loading));
		case DIALOG_ERROR:
			return (new AlertDialog.Builder(this)).setMessage(R.string.leaderboard_error)
					.setPositiveButton(R.string.too_bad, null).create();
		}
		return null;
	}

	public void onResume() {
		super.onResume();
		
		final RequestControllerObserver observer = new RequestControllerObserver() {

			@Override
			public void requestControllerDidReceiveResponse(final RequestController requestController) {
				Log.d("-------------", "REQUEST RECIEVED "+requestController.toString());
				// get the scores from our controller
				scoresController = (ScoresController) requestController;
				final List<Score> scores = scoresController.getScores();
				User user;
				

				// set up the leaderboard as string array
				final List<String> scoreList = new ArrayList<String>();
				int i = 0;
				for (final Score score : scores) {
					scoreList.add(++i + ". " + score.getUser().getDisplayName() + " DOB: "+score.getUser().getDateOfBirth()+" - " + ScoreFormatter.format(score));
					
					final RequestControllerObserver observer2 = new RequestControllerObserver() {

						@Override
						public void requestControllerDidFail(RequestController arg0, Exception arg1) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void requestControllerDidReceiveResponse(RequestController arg0) {
							// TODO Auto-generated method stub
							
						}
						//UserController myUserController = new UserController(observer2);
				
					};
				}
				Log.d("-------------", "55555555");

				// find the list
				final ListView list = (ListView) findViewById(R.id.leaderboard_list);
				Log.d("-------------", "6666666666666");

				// set up an adapter for our list view
				final ListAdapter adapter = new ArrayAdapter<String>(LeaderBoard4.this, android.R.layout.simple_list_item_1, scoreList);
				Log.d("-------------", "77777777777");
				// put the adapter into the list
				list.setAdapter(adapter);
				Log.d("-------------", "88888888888");
				// we're done!
				dismissDialog(DIALOG_PROGRESS);
			}

			@Override
			public void requestControllerDidFail(final RequestController aRequestController, final Exception anException) {
				Log.d("-------------", "REQUEST FAILED "+aRequestController.toString());
				if(aRequestController==scoresController){
					Log.d("-------------", "9999999999999999");
				}
				Log.d("-------------", "AAAAAAAAAAAAAAAAAAAAAA");
				dismissDialog(DIALOG_PROGRESS);
				showDialog(DIALOG_ERROR);
			}
		};

		// set up a ScoresController with our observer
		ScoresController scoresController = new ScoresController(observer);
		Log.d("-------------", "11111111111");

		// show a progress dialog while we're waiting
		showDialog(DIALOG_PROGRESS);
		Log.d("-------------", "2222222222222");

		// we want to get the top 10 entries...
		scoresController.setRangeLength(50);
		Log.d("-------------", "33333333333333333333");
		
		// starting from the first place
		scoresController.loadRangeAtRank(1);
		Log.d("-------------", "44444444444444444444");
	}
}
